﻿using System;
using System.Drawing;
using System.Windows;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Media;
using ScrumTable.UI.View.Extension;
using ScrumTable.UI.ViewModel.Extension;
using Point = System.Windows.Point;

namespace ScrumTable.UI.View.WinTouch.UserControls
{
	/// <summary>
	/// Interaction logic for Marker.xaml
	/// </summary>
	public partial class LoadScreen
	{ 

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets or sets a list of current loading items 
        /// </summary>
        /// <value> a list of current loading items</value>
        public ObservableCollection<LoadingItem> ItemsToLoad { get; private set; }
        

        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------
   
        public LoadScreen()
		{
            ItemsToLoad = new ObservableCollection<LoadingItem>();         
            DataContext = this;
		    InitializeComponent();
        }

        #endregion 

        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------
 
        /// <summary>
        /// Calling this method determins that a new sub import progress has
        /// been started. This means that an additional progress bar should
        /// be displayed.
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        /// references to the ReportProgress / EndSubProgress methods.</param>
        public void StartSubProgress(string identifier)
        {
            if (!string.IsNullOrEmpty(identifier))
            {
                ItemsToLoad.Add(new LoadingItem() { Identifier = identifier, Value = 0 });
            }
        }

        /// <summary>
        /// Reports a progress associated to an identifier (handle).
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        /// references to the StartSubProgress / EndSubProgress methods.</param>
        /// <param name="current">Specifies the current state in percent. The maximum is 100.</param>
        public void ReportProgress(string identifier, byte current)
        {
            var selectedItems = from y in ItemsToLoad where y.Identifier == identifier select y;
            var selectedItem = selectedItems.FirstOrDefault();
            if (selectedItem != null)
            {
                selectedItem.Value = current;
            }
        }

        /// <summary>
        /// Reports the end of a progress associated to an identifier (handle).
        /// </summary>
        /// <param name="identifier">Specifies the import identifier (handle). This value
        /// references to the StartSubProgress / ReportProgress methods.</param>        
        /// <param name="e">Exception e if something went wrong</param>
        public void EndSubProgress(string identifier, Exception e)
        {
            if (e != null)
            {
                ItemsToLoad.Clear();
                return;
            }
            var selectedItems = from y in ItemsToLoad where y.Identifier == identifier select y;
            var selectedItem = selectedItems.FirstOrDefault();
            if (selectedItem != null)
            {
                ItemsToLoad.Remove(selectedItem);
            }
        }
        #endregion

        #region Nested Classes
        //--------------------------------------------------------------------
        // Nested Classes
        //--------------------------------------------------------------------
        /// <summary>
        /// Descripes a single item which is currently loading. 
        /// </summary>
        public class LoadingItem : DependencyObject
        {
            /// <summary>
            /// Gets or sets the identifier of the loading item.
            /// </summary>
            /// <value>The identifier.</value>
            public string Identifier {get;set;}

            /// <summary>
            /// Sets the progress in  %
            /// </summary>
            /// <value> the progress in %.</value>
            public byte Value
            {
                set
                {
                    
                    double value1 = (((value - 2) >= 0) ? (value - 2) : value) / 100.0;
                    double value2 = (((value + 2) <= 100) ? (value + 2) : value) / 100.0;
                    var brush = new LinearGradientBrush() { StartPoint = new Point(0.0, 0.5), EndPoint = new Point(1, 0.5) };
                    brush.GradientStops.Add(new GradientStop() { Color = ColorExtensions.ColorFromString("#FFDAFF8F"), Offset = 0 });
                    brush.GradientStops.Add(new GradientStop() { Color = ColorExtensions.ColorFromString("#FF42FF00"), Offset = value1 });
                    brush.GradientStops.Add(new GradientStop() { Color = ColorExtensions.ColorFromString("#FFFE4F00"), Offset = value2 });
                    brush.GradientStops.Add(new GradientStop() { Color = ColorExtensions.ColorFromString("#FFFFBAB7"), Offset = 1 });
                    brush.GradientStops.Add(new GradientStop() { Color = ColorExtensions.ColorFromString("#FF00FCED"), Offset = value / 100.0 });
                    
                    ItemBrush = brush;
                }
            }

            /// <summary>
            /// only for GUI
            /// </summary>
            /// <value>The item brush.</value>
            public LinearGradientBrush ItemBrush
            {

                get { return (LinearGradientBrush)GetValue(ItemBrushProperty); }
                set { SetValue(ItemBrushProperty, value); }
               
            }

 
            /// <summary>
            /// Only for the GUI
            /// </summary>
            /// <value></value>

            public static readonly DependencyProperty ItemBrushProperty =
            DependencyProperty.Register("ItemBrush",
                                        typeof(LinearGradientBrush),
                                        typeof(LoadingItem),
                                        new UIPropertyMetadata(null));

        }

        #endregion
    }
}